<?php
namespace Home\Controller;

use MyBase\Tool;
use MyBase\BaseController;


class SearchController extends BaseController{

	public function search(){
		$getData = I("get.");
		// print_r($getData);
		if(empty($getData['content'])){
			$content = "";
		}else{
			$content = $getData['content'];
		}
		//确定页码
		if(empty($getData['p'])){
			$page_num = 1;
		}else{
			$page_num = $getData['p'];
		}
		$pageLimit = 3;
		//如果是菜谱
		if($getData['type'] == 4){
			$where['cookbook_name'] =  array('like',"%".$content."%");
			$where['cookbook_decription'] =  array('like',"%".$content."%");
			$where['_logic'] = 'or';
			//获取分页项
	    	$count = $this->CookBookModel->where($where)->count();
	    	$Page = new \Think\Page($count,$pageLimit);
	    	$show = $Page->show();
	    	$data = $this->CookBookModel->where($where)->page($page_num,$pageLimit)->select();
	    	foreach ($data as $key => $value) {
	    		$data[$key]['cookbook_photo'] = __ROOT__.'/Uploads/cookbook/'.$value['cookbook_add_time'].$value['cookbook_id'].'/'.$value['cookbook_photo'];;
	    	}
	        // print_r($data);
	    	$this->assign('cookbook_data',$data);
	    	$this->assign('cookbook_page',$show);

		}else{
			$where['goods_name'] =  array('like',"%".$content."%");
			$where['goods_content'] =  array('like',"%".$content."%");
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
			$map['goods_status'] = array('eq','Y');
			$map['goods_type'] = array('eq',$getData['type']);

			//获取分页项
	    	$count = $this->GoodsModel->where($where)->count();
	    	$Page = new \Think\Page($count,$pageLimit);
	    	$show = $Page->show();
	    	//得到分页数据
	    	$data = $this->GoodsModel->where($map)->page($page_num,$pageLimit)->select();
	    	//得到类别数据
	    	$typeData = $this->GoodsTypeModel->getGoodsTypeList();
	    	$typeDataArr = array();
	    	foreach ($typeData as $type_k => $type_v) {
	    		$typeDataArr[$type_v['goods_type_id']] = $type_v['goods_type_name'];
	    	}

	    	$indexController = A("Index");
	        foreach ($data as $k => $v) {
	        	$data[$k]['goods_type'] = $typeDataArr[$data[$k]['goods_type']];
	            $data[$k]['goods_pic'] = $indexController->formatFoodPic($data[$k]['goods_pic'],$v,true);
	        }
	        
	    	$this->assign('food_data',$data);
	    	$this->assign('food_page',$show);			
		}
		//最近热卖
        $hotColumns = array('goods_id','goods_name','goods_pv');
        $recenterHot = $this->GoodsModel->getGoodsByCondition(array('goods_status'=>'Y'),$hotColumns,array('goods_add_time'=>'DESC'),9);
        $this->assign('recenterHot',$recenterHot);
        $indexController = A("Index");
        $scanfData = $indexController->getLatestScanf();
        $this->assign('scanfData',$scanfData);
		$this->assign('position',array(__MODULE__=>'首页',__ACTION__=>'搜索结果'));
		if($getData['type'] == 4 )
			$this->display("Index/search_cookbook");
		else
			$this->display("Index/search_food");
	}

}